/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * itemsDialog.java
 *
 * Created on 2011-5-11, 14:51:57
 */
package junchangmis.student;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import junchangmis.*;
import junchangmis.codecSetting.*;
import jxl.write.WriteException;
import publicmethod.*;
import java.util.Vector;
import javax.swing.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.StringTokenizer;

/**
 *
 * @author developers
 */
public class itemsDialog extends javax.swing.JDialog {

    MainFrame parent;
    Vector<formalStu> stuName;
    DefaultListModel stuListModel = new DefaultListModel();
    Vector<String> names;
    Vector<String> classCodes;
    Vector<String> tels;
    String md;

    /** Creates new form itemsDialog */
    public itemsDialog(java.awt.Frame parent, boolean modal, MainFrame pparent, Vector<String> names, Vector<String> classCodes, Vector<String> tels, String md) {//按生日查询时的构造函数
        super(parent, modal);
        this.parent = pparent;
        this.names = names;
        this.classCodes = classCodes;
        this.tels = tels;
        this.md = md;
        initComponents();
        this.setLocationRelativeTo(null);
        this.setTitle("学生列表");

        this.jListFormalStu.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        this.updateListBirth();
    }

    public itemsDialog(java.awt.Frame parent, boolean modal, MainFrame pparent, Vector<formalStu> stuName) {//按名字查询时的构造函数
        super(parent, modal);
        this.parent = pparent;
        this.stuName = stuName;
        initComponents();
        this.setLocationRelativeTo(null);
        this.setTitle("学生列表");
        this.jButtonConfirm.setVisible(false);
        this.jCheckBoxPrint.setVisible(false);

        this.jListFormalStu.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        this.jListFormalStu.setToolTipText("双击条目即可修改");


        this.jListFormalStu.addMouseListener(new MouseAdapter() {

            public void mouseClicked(MouseEvent e) {
                if (jListFormalStu.getSelectedIndex() != -1) {
                    if (e.getClickCount() == 1) {
                        oneNewClick(jListFormalStu.getSelectedValue());
                    }
                    if (e.getClickCount() == 2) {
                        twoNewClick(jListFormalStu.getSelectedValue());
                    }
                }
            }
        });
        updateList();//填充学生列表
    }

    void updateListBirth() {//按生日查询时更新列表
        this.stuListModel.removeAllElements();
        for (int i = 0; i < names.size(); i++) {
            String translate = codecMethodNew.getMeaning(classCodes.get(i).substring(0, 7));
            this.stuListModel.addElement(names.get(i) + "   ,   " + translate + "  ,  " + tels.get(i));
            this.jListFormalStu.updateUI();
        }
    }

    void updateList() {//按姓名查询时更新列表
        //将正式生显示在右侧列表中
        this.stuListModel.removeAllElements();
        for (int i = 0; i < stuName.size(); i++) {
            this.stuListModel.addElement(stuName.get(i).id + " "
                    + stuName.get(i).name + " "
                    + stuName.get(i).sex + " "
                    + stuName.get(i).birthday + " "
                    + stuName.get(i).tel + " "
                    + stuName.get(i).school);
            this.jListFormalStu.updateUI();
        }
    }

    void oneNewClick(Object value) {
    }

    void twoNewClick(Object value) {
        System.out.println("同名学生选择,双击");
        //提取主键
        StringTokenizer st = new StringTokenizer(this.jListFormalStu.getSelectedValue().toString());
        Vector<String> temp = new Vector<String>();
        while (st.hasMoreTokens()) {
            //System.out.println(st.nextToken());
            temp.add(st.nextToken());
        }
        String id = temp.get(0);
        formalStu student = new formalStu();
        stuChooseMethod.hasFormalStu(id, student);
        formalStuInputUI fr = new formalStuInputUI(this.parent, true, this.parent, student);
        //dispose();//关闭自己
        fr.setVisible(true);//显示子窗口

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jListFormalStu = new javax.swing.JList();
        jButtonConfirm = new javax.swing.JButton();
        jCheckBoxPrint = new javax.swing.JCheckBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jListFormalStu.setFont(new java.awt.Font("华文细黑", 0, 24)); // NOI18N
        jListFormalStu.setModel(this.stuListModel);
        jScrollPane1.setViewportView(jListFormalStu);

        jPanel1.add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(-4, 10, 1030, 338));

        jButtonConfirm.setFont(new java.awt.Font("华文细黑", 0, 24)); // NOI18N
        jButtonConfirm.setText("确定");
        jButtonConfirm.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonConfirmActionPerformed(evt);
            }
        });
        jPanel1.add(jButtonConfirm, new org.netbeans.lib.awtextra.AbsoluteConstraints(340, 410, -1, -1));

        jCheckBoxPrint.setFont(new java.awt.Font("华文细黑", 0, 24)); // NOI18N
        jCheckBoxPrint.setText("是否打印");
        jPanel1.add(jCheckBoxPrint, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 360, -1, -1));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 783, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(13, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonConfirmActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonConfirmActionPerformed
        try {
            // TODO add your handling code here:
            birthdayExcel work = new birthdayExcel(names, classCodes, tels, md);
            String filename = "生日统计表\\";
            filename = filename + "生日" + "(" + md + ")" + ".xls";
            work.setOutputFile(filename);
            work.write();
            System.out.println("Please check the result file " + filename);
            JOptionPane.showConfirmDialog(null, "生成成功！", "提示", JOptionPane.CLOSED_OPTION, JOptionPane.INFORMATION_MESSAGE);
            if (this.jCheckBoxPrint.isSelected() == true) {
                PrintTest.print(PrintTest.getPath() + "\\" + filename);
            } else {
                PrintTest.showTable(PrintTest.getPath() + "\\" + filename);
            }
            dispose();
        } catch (IOException ex) {
            Logger.getLogger(itemsDialog.class.getName()).log(Level.SEVERE, null, ex);
        } catch (WriteException ex) {
            Logger.getLogger(itemsDialog.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jButtonConfirmActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
        itemsDialog dialog = new itemsDialog(new javax.swing.JFrame(), true);
        dialog.addWindowListener(new java.awt.event.WindowAdapter() {
        public void windowClosing(java.awt.event.WindowEvent e) {
        System.exit(0);
        }
        });
        dialog.setVisible(true);
        }
        });*/
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonConfirm;
    private javax.swing.JCheckBox jCheckBoxPrint;
    private javax.swing.JList jListFormalStu;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
}
